from django.http import JsonResponse
from .track.SatelliteTrack import SatelliteTrack


def transit(request):
    """
    过境预测
    :param request:
    :return:
    """
    if request.method == "GET":
        line1 = request.GET.get("line1")
        line2 = request.GET.get("line2")
        start_time = request.GET.get("start_time")
        end_time = request.GET.get("end_time")
        lat = request.GET.get("lat")
        lon = request.GET.get("lon")
        altitude_degrees = request.GET.get("altitude_degrees", 10)
        elevation_m = request.GET.get("elevation_m", 400)

        if int(end_time) - int(start_time) > 7 * 24 * 3600:
            return JsonResponse({
                "code": 1500,
                "message": "超出最大区间，最大为7天"
            })

        satellite_track = SatelliteTrack()
        satellite_name = "satellite"
        satellite_track.add_satellite_from_line_data(line1, line2, satellite_name)

        res = satellite_track.transit(
            satellite_name=satellite_name,
            start_time=start_time,
            end_time=end_time,
            lat=float(lat),
            lon=float(lon),
            elevation_m=float(elevation_m),
            altitude_degrees=int(altitude_degrees)
        )

        return JsonResponse({
            "code": 0,
            "message": "成功",
            "data": {
                "res": res
            }
        }, safe=False)


def get_satellite_gps(request):
    """
    获取卫星的星下点(通常用于卫星轨迹绘制)
    :param request:
    :return:
    """
    if request.method == "GET":
        line1 = request.GET.get("line1")
        line2 = request.GET.get("line2")
        start_time = request.GET.get("start_time")
        end_time = request.GET.get("end_time")
        step_time_s = int(request.GET.get("step_time_s"), 60)

        satellite_name = "satellite"
        satellite_track = SatelliteTrack()
        satellite_track.add_satellite_from_line_data(line1, line2, satellite_name)

        if ((int(end_time) - int(start_time)) / step_time_s) > 10000:
            return JsonResponse({
                "code": 1500,
                "message": "最多一万个轨迹点"
            })

        gps_list = satellite_track.get_satellite_gps(
            satellite_name=satellite_name,
            start_time=start_time,
            end_time=end_time,
            step_time_s=step_time_s
        )

        return JsonResponse({
            "code": 0,
            "data": {
                "gps_list": gps_list
            }
        })


def satellite_differance(request):
    """
    计算卫星相对于观察者的位置
    :param request:
    :return:
    """
    if request.method == "GET":
        line1 = request.GET.get("line1")
        line2 = request.GET.get("line2")
        start_time = request.GET.get("start_time")
        end_time = request.GET.get("end_time")
        lat = request.GET.get("lat")
        lon = request.GET.get("lon")
        elevation_m = request.GET.get("elevation_m", 400)

        if int(end_time) - int(start_time) > 3600:
            return JsonResponse({
                "code": 1500,
                "message": "超出最大区间，最大为3600秒"
            })

        satellite_name = "satellite"
        satellite_track = SatelliteTrack()
        satellite_track.add_satellite_from_line_data(line1, line2, satellite_name)

        res = satellite_track.get_position(
            satellite_name=satellite_name,
            start_time=start_time,
            end_time=end_time,
            lat=float(lat),
            lon=float(lon),
            elevation_m=float(elevation_m)
        )

        return JsonResponse({
            "code": 0,
            "data": {
                "res": res
            }
        }, safe=False)
